﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using org.osgi.framework.launch;

namespace test.command
{
    class UpdateBundleTestCommand : ICommand
    {
        private Framework framework;
        public UpdateBundleTestCommand(Framework framework)
        {
            this.framework = framework;
        }

        public string GetCommandName()
        {
            return "test";
        }

        public string GetHelpText()
        {
            return "测试Bundle的更新";
        }

        public string GetDetailHelpText()
        {
            return "测试Bundle的更新";
        }

        public string ExecuteCommand(string commandLine)
        {
            Process process = Process.GetCurrentProcess();

            Int64 preWorkingSet = process.WorkingSet64;

            String args = commandLine.Substring(GetCommandName().Length).Trim();
            String[] argArray = args.Split(new Char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            Int64 bundleId = Int64.Parse(argArray[0]);
            Int64 testTimes = Int64.Parse(argArray[1]);

            for (int i = 0; i < testTimes; i++)
            {
                framework.getBundleContext().getBundle(bundleId).update();
            }
            Int64 afterWorkingSet = process.WorkingSet64;
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("之前".PadLeft(12) + "之后".PadLeft(12));
            sb.AppendLine("内存" + preWorkingSet.ToString().PadLeft(8) + afterWorkingSet.ToString().PadLeft(12));
            return sb.ToString();
        }
    }
}
